<?php

namespace app\common\service;

use app\common\model\AdminUser;
use think\facade\Db;

class Lists
{

    public function GetList($code, $Lists)
    {
        $data = [];
        foreach ($Lists as $k => $v) {
            if ($v['num'] == $code) {
                $data = $v;
            }
        }
        return $data;
    }

    public function ListMenu($Lists)
    {
        $data = [];
        foreach ($Lists as $k => $v) {
            $data[] = ['title' => $v['name'], 'code' => $v['num'], 'on' => false, 'num' => 0];
        }
        return $data;
    }

    public function ListTotal($code, $Lists)
    {
        $data = [];
        $Item = $this->GetList($code, $Lists);
        $cols = $Item['table_cols'];
        foreach ($cols as $k2 => $v2) {
            $total = !empty($v2['total']) ? $v2['total'] : 2;
            if ($total == 1) {
                $data[] = ['code' => $v2['field'], 'title' => $v2['title'], 'num' => 0];
            }
        }
        return $data;
    }

    public function SetWhere($code, $Lists)
    {

        $status = 2;
        $Item = $this->GetList($code, $Lists);
        $where = !empty($Item['set_where']) ? $Item['set_where'] : "1=1";
        if (GetToken('is_spuer') == 1) {
            return $where;
        }
        $PowerWhere = '';
        $power_role = !empty($Item['power_role']) ? $Item['power_role'] : '';
        if ($power_role) {
            $status = $this->PowerSetWhere('role_ids', $power_role);
        }
        $power_department = !empty($Item['power_department']) ? $Item['power_department'] : '';
        if ($power_department && $status == 2) {
            $status = $this->PowerSetWhere('department_ids', $power_department);
        }
        if ($status == 2) {
            $power_field = !empty($Item['power_field']) ? $Item['power_field'] : [];
            $adminId = GetToken('id');
            foreach ($power_field as $k => $v) {
                $is_path = !empty($v['is_path']) ? $v['is_path'] : 2;
                if ($is_path == 1) {
                    $uid = $adminId . $this->UserPower($adminId);
                } else {
                    $uid = $adminId;
                }
                if ($PowerWhere) {
                    $PowerWhere .= " or a." . $v['field'] . " IN(" . $uid . ")";
                } else {
                    $PowerWhere .= " and (a." . $v['field'] . " IN(" . $uid . ")";
                }
            }
            if ($PowerWhere) {
                $PowerWhere .= ")";
            }
        }
        return $where . $PowerWhere;
    }

    public function PowerSetWhere($field, $power)
    {
        $status = 2;
        $ids = GetToken($field);
        $arr = explode(',', $power);
        foreach ($arr as $k => $v) {
            if ($ids == $v) {
                $status = 1;
            }
        }
        return $status;
    }

    //权限无限极
    public function UserPower($adminid = '')
    {
        $AdminUser = new AdminUser();
        $uid = '';
        $arr = $AdminUser->where("path_id='" . $adminid . "'")->field("id")->select()->toArray();
        foreach ($arr as $k => $v) {
            $uid .= "," . $v['id'];
            $uid .= $this->UserPower($v['id']);
        }
        return $uid;
    }


    public function QueryData($code, $Lists)
    {
        $search = [
            'like' => '',
            'datatime' => '',
            'equal' => '',
        ];
        $Item = $this->GetList($code, $Lists);
        $cols = $Item['table_cols'];
        foreach ($cols as $k => $v) {
            $se = !empty($v['search']) ? $v['search'] : '';
            $fields = explode('__', $v['field']);
            if (count($fields) == 1) {
                $v['field'] = 'a.' . $v['field'];
            } else {
                $v['field'] = $fields[0] . '.' . $fields[1];
            }
            switch ($se) {
                case "like":
                    $search['like'] .= $v['field'] . '|';
                    break;
                case "datetime":
                    $search['datatime'] .= $v['field'] . '|';
                    break;
                case "select":
                case "url":
                    $search['equal'] .= $v['field'] . '|';
                    break;
            }
        }
        $search = [
            'like' => rtrim($search['like'], "|"),
            'datatime' => rtrim($search['datatime'], "|"),
            'equal' => rtrim($search['equal'], "|"),
        ];


        return $search;
    }

    public function QueryWhere($code, $Lists, $params)
    {
        $search = $this->QueryData($code, $Lists);
        $where = [];
        if (!empty($params['keyword']) && $search['like']) {
            $where[] = [$search['like'], 'like', '%' . $params['keyword'] . '%'];
        }


        if (!empty($params['search_time_field']) && !empty($params['search_time_field_val']) && $search['datatime']) {
            $times = explode('至', $params['search_time_field_val']);
            $start_time = trim($times[0]);
            $end_time = trim($times[1]);
            $time_field = $params['search_time_field'];
            if (!empty($start_time)) {
                $where[] = [$time_field, '>=', strtotime($start_time)];
            }
            if (!empty($end_time)) {
                $where[] = [$time_field, '<=', strtotime($end_time)];
            }
        }
        $equal = explode('|', $search['equal']);
        foreach ($equal as $k => $v) {
            if ($v) {
                $t = explode('.', $v);
                if (!empty($params[$t[1]])) {
                    $where[] = [$v, '=', $params[$t[1]]];
                }
            }
        }
        if (!empty($params['choose_ids'])) {
            $where[] = ['a.id', 'in', $params['choose_ids']];
        }
        return $where;
    }

    public function ListRaw($code, $Lists, $params)
    {
        $Item = $this->GetList($code, $Lists);
        $raw = !empty($Item['order_raw']) ? $Item['order_raw'] : 'id desc';
        if (!empty($params['raw'])) {
            $raw = $params['raw'];
        }
        return $raw;
    }

    public function ListSource($code, $Lists)
    {
        $data = [];
        $Item = $this->GetList($code, $Lists);
        $cols = $Item['table_cols'];
        foreach ($cols as $k => $v) {
            if (!empty($v['options'])) {
                $options = explode('|', $v['options']);
                $data[$options[0]] = GetOptions($v['options']);
            }
        }
        return $data;
    }

    public function ListField($code, $Lists, $type)
    {
        $data = [];
        $Item = $this->GetList($code, $Lists);
        $cols = $Item['table_cols'];
        $fields = [];
        foreach ($cols as $k => $v) {
            $fields[] = [
                'field' => $v['field'],
                'title' => $v['title']
            ];
        }
        if ($type == 1) {
            $field = '';
            foreach ($fields as $k => $v) {
                $fields = explode('__', $v['field']);
                if (count($fields) == 1) {
                    $v['field'] = 'a.' . $v['field'];
                } else {
                    $v['field'] = $fields[0] . '.' . $fields[1] . ' as ' . $v['field'];
                }
                $field .= $v['field'] . ',';
            }
            $fields = rtrim($field, ",");
        }
        return $fields;

    }

    public function ExcelField($form)
    {
        $layout = $form['layout'];
        $fields = [];
        foreach ($layout as $k => $v) {
            $fields[] = [
                'field' => $v['field'],
                'title' => $v['title']
            ];
        }
        return $fields;
    }

    public function ListData($rows)
    {
        foreach ($rows as $k => $v) {
            $item = $v;
            foreach ($item as $k2 => $v2) {
                if ($k2 == 'create_id' || $k2 == 'update_id') {
                    $user = DB::name("admin_user")->where("id='" . $v2 . "'")->field("name")->find();
                    switch ($k2) {
                        case "create_id":
                            $rows[$k]['create_name'] = !empty($user['name']) ? $user['name'] : '';
                            break;
                        case "update_id":
                            $rows[$k]['update_name'] = !empty($user['name']) ? $user['name'] : '';
                            break;
                    }
                }
                if ($k2 == 'flow__status') {
                    $rows[$k]['flow__status'] = !empty($v['flow__status']) ? $v['flow__status'] : 1;
                }
            }
        }
        return $rows;
    }


}